package com.lilicould.service;

import com.google.gson.Gson;

import com.lilicould.dao.TeacherDao;
import com.lilicould.dao.UserDao;

import com.lilicould.entity.TeacherDetails;
import com.lilicould.entity.Teachers;
import com.lilicould.entity.Users;
import com.lilicould.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.mindrot.jbcrypt.BCrypt;

public class TeacherService {

    // 获取教师信息
    public String getTeacher(String userNo) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            TeacherDao teacherDao = sqlSession.getMapper(TeacherDao.class);
            TeacherDetails teacher = teacherDao.getTeacherDetailsByUserNo(userNo);
            if (teacher != null) {
                return new Gson().toJson(teacher);
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    // 获取所有老师信息
    public String getAllTeachers() {
        Gson gson = new Gson();
        try(SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            return gson.toJson(sqlSession.getMapper(TeacherDao.class).getTeacherDetails());
        }catch (Exception e) {
            return null;
        }
    }

    public String addTeacher(TeacherDetails teacherDetails) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            // 获取映射器对象
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            TeacherDao teacherDao = sqlSession.getMapper(TeacherDao.class);

            // 添加用户
            Users user = new Users(teacherDetails.getUserNo(), BCrypt.hashpw("123456", BCrypt.gensalt()), "teacher", teacherDetails.getName(), null, null);
            Teachers teacher = new Teachers(teacherDetails.getUserNo(), teacherDetails.getHireYear(), teacherDetails.getCollegeCode(),teacherDetails.getTitle());
            int result = userDao.addUser(user);
            if (result > 0) {
                result = teacherDao.addTeacher(teacher);
                if (result > 0) {
                    sqlSession.commit();
                    return null;
                } else {
                    return "添加教师信息时失败";
                }
            } else {
                return "添加用户信息时失败";
            }
        } catch (Exception e) {
            return e.getMessage();
        }

    }

    public String deleteTeacher(String userNo) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            // 获取映射器对象
            TeacherDao teacherDao = sqlSession.getMapper(TeacherDao.class);
            UserDao userDao = sqlSession.getMapper(UserDao.class);

            // 删除用户
            // 此处一定要先删除老师信息，再删除用户信息，否则会报外键约束错误
            int result = teacherDao.deleteTeacher(userNo);
            if (result > 0) {
                result = userDao.deleteUser(userNo);
                if (result > 0) {
                    sqlSession.commit();
                    return null;
                }else
                    return "删除用户信息失败";
            }else {
                return "删除教师信息失败";
            }
        }catch (Exception e) {
            return e.getMessage();
        }
    }

}
